<?php
class FormsAttr extends BaseAttr {
	public function FormsAttr($attr, $lang=null, $mutil_lang=false){
		parent::BaseAttr($attr, $lang, $mutil_lang);
		
	}
	
	protected function init_data(){
// 		$name = $this->attr['forms'];
		$name = $this->attr['src_id'];
		$model_name = $this->formHelper->model_name;
		$form_name = $this->formHelper->item_lists[$name]['forms'];
		$form_mod = new FormsModel();
		$form = $form_mod->get_by_attrs(array('name'=>$form_name));
		$list_view = $form['list_view'];
		//获取值
		$items = $this->formHelper->form['items'];
		foreach ($list_view as $k=>&$item_name){
// 		print_r($items);exit;
			$item = $items[$k];
			if($item['mutil_lan']){
				//多语言
				$item_name = $item[$this->mutil_lang]['name'];
			}else{
				//单一语言
				$item_name = $item['name'];
			}
		}
		
		if($this->mutil_lang!==false){
			$values = $this->formHelper->entity[$name][$this->mutil_lang];
		}else{
			$values = $this->formHelper->entity[$name];
		}
		$values = SortHelper::sortByCol($values, 'rank');
// 		print_r($this->formHelper->entity[$name]);exit;
		return array(
			'list_titles' => $list_view,
			'list_data' => $values,
		);
	}
	
	public function get_post_val(&$getted_post_data){
		$name = $this->attr['id'];
		
		$data = array();
		if($this->mutil_lang!==false){
			foreach ($this->formHelper->langs as $lang){
				if(!isset($_POST[$lang . '_' . $name])) return false;
				$temp_item_ids = $_POST[$lang . '_' . $name];
				foreach ($temp_item_ids as $temp_item_id){
					$temp_data = $this->get_item_data($name, $temp_item_id, $lang);
					if($temp_data === null)continue;
					$data[$lang][$temp_item_id] = $temp_data;
					$getted_post_data[$name][$lang][$temp_item_id] = $temp_data;
				}
			}
		}else{
			if(!isset($_POST[$name])) return false;
			$temp_item_ids = $_POST[$name];
			foreach ($temp_item_ids as $temp_item_id){
				$temp_data = $this->get_item_data($name, $temp_item_id);
				if($temp_item_id === null)
					continue;
				$data[$temp_item_id] = $temp_data;
				$getted_post_data[$name][$temp_item_id] = $temp_data; 
			}
		}
		return $data;
	}
	
	private function get_item_data($name, $item_id, $lang=null){
		$temp_mod = new TempModel();
		//尝试从temp中获取
		if($temp_mod->exists_key($item_id)){
			return $temp_mod->get_by_key($item_id);
		}
		//尝试从主表中获取
		if(isset($_POST['id']) && !empty($_POST['id'])){
			$id = $_POST['id'];
			$model_name = $this->formHelper->model_name;
			$mod = &m($model_name);
			$d = $temp_mod->get_by_key($id);
			if($lang === null){
				if(isset($d[$name][$item_id])){
					return $d[$name][$item_id];
				}
			}else{
				if(isset($d[$lang][$name][$item_id])){
					return $d[$lang][$name][$item_id];
				}
			}
		}
		return null;
	}
}

?>